import beads.*;
import oscP5.*;
import rwmidi.*;
import processing.serial.*;
AudioContext ac;
Serial[] myPort;
PFont font;
int sP;
float curveD=2;
Mod rootMod;
ModWindow[] uic; 
int r=10;
int wO=5;
int noc;
int uidNum=0;
int pGap=16;
float icc;
color[] sysCl;
Panner leftOut, rightOut;
oscIOHandler[] oscHandler;
componentLibrary cp;

PGraphics icon;
int[] serialList;


void setup() {
  size(1000, 850, JAVA2D);
  
  ac = new AudioContext();
  oscHandler=new oscIOHandler[8];
  oscHandler[0]=new oscIOHandler("127.0.0.1",8080,8000,"default");
  oscHandler[1]=new oscIOHandler("127.0.0.1",5354,9999,"2");
  oscHandler[0].active=true;
  leftOut=new Panner(ac, -1.0);
  rightOut=new Panner(ac, 1.0);
  ac.out.addInput(leftOut);
  ac.out.addInput(rightOut);
  background(64);
  sysCl=new color[6];
  sysCl[0]=color(150);
  sysCl[1]=color(255, 90, 0);
  sysCl[2]=color(255, 140, 0);
  sysCl[3]=color(0, 210, 255);
  sysCl[4]=color(0, 220, 0);
  sysCl[5]=color(255, 255, 255);
  icon = createGraphics(64, 64, JAVA2D);
  //move to draw for animated icon
  icon.beginDraw();
  icon.smooth();
  icon.noFill();
  icon.strokeWeight(6);
  icon.strokeCap(PROJECT);
  icon.stroke(sysCl[1]);
  icon.line(18, 22, 18, 42);
  icon.stroke(sysCl[2]);
  icon.ellipse(42, 32, 20, 20);
  icon.endDraw();
  frame.setIconImage(icon.image);
  frame.setResizable(true);
  frame.setTitle("[untitled]*");
  font = loadFont("Corbel-Bold-10.vlw");
  myPort=new Serial[8];
  cp=new componentLibrary();
  rootMod=new modContainer(0);
  uic=new ModWindow[255];
  serialList=new int[8];
  println(Serial.list());

  uic[0]=new ModWindow(5, 5, 800, 500, 1);
  //uic[1]=new ModContainer(5, 510, 1000, 500, 2);
  uic[0].setParent(rootMod);
  
  noc=1;
  
  
}

void draw() {

  background(150);
  uic[0].drawAll();
  //uic[1].drawAll();
  //mC[1].drawAll(1);
  
}

void mousePressed() {
  uic[0].pressed();
}
void mouseReleased() {
  uic[0].released();
}
void keyPressed() {
  if (key == ESC) {
    key = 0;  // Fools! don't let them escape!
  }
  uic[0].keyPress((int)key);
}

String floatInput(int key, String iS) {
  Boolean minus=false;
  Boolean hasDP=false;
  if (!iS.equals("") && iS.charAt(0)=='-') {
    iS=iS.substring(1, iS.length());
    minus=true;
  }        
  if (iS.equals("")) {
    iS="0";
  }
  else {
    int c=0;
    do {
      if (iS.charAt(c)=='.')hasDP=true;
      c++;
    }
    while (c<iS.length () && !hasDP);
  }
  if (key==46) {
    if (!hasDP) {
      if (iS.equals("0")) {
        iS="0.";
      }
      else {
        iS=iS+str(char(46));
      }
      hasDP=true;
    }
  }
  else if (key>47 && key<58) {
    if (iS.length()==1 && iS.charAt(0)=='0') {
      iS=str(char(key));
    }
    else {
      iS=iS+str(char(key));
    }
  }
  else if (key==DELETE) {
    iS="";
    minus=false;
  }
  else if (key==BACKSPACE) {
    if (iS.length()>1) {
      iS=iS.substring(0, iS.length()-1);
    }
    else {
      iS="";
    }
  }
  if (key==43) {
    minus=false;
  }
  if (key==45) {
    minus=true;
  }
  if (minus) {
    iS="-"+iS;
  }
  return iS;
}
String stringInput(int key, String iS) {
   //need to filter out some keys
  if (key==DELETE) {
    iS="";
  }
  else if (key==BACKSPACE) {
    if (iS.length()>1) {
      iS=iS.substring(0, iS.length()-1);
    }
    else {
      iS="";
    }
  }else if(key!=65535){
    iS=iS+str(char(key));
  }
  println(iS);
  return iS;
}

